/*
  Joystick Module

  created 18 Dec 2018
  by MisterBotBreak

  This example code can be found:

  https://create.arduino.cc/projecthub/MisterBotBreak/how-to-use-a-joystick-with-serial-monitor-1f04f0
*/

int VRx = PA0; //Analog Pin for X Position
int VRy = PA1; //Analog Pin for Y Position
int SW = PA4;   //Digital Pin for Button

//Store State Variables
int xPosition = 0; 
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;

// STM32 ADC引脚 
void setup() {
  Serial.begin(9600); 
  
  pinMode(VRx, INPUT_ANALOG);
  pinMode(VRy, INPUT_ANALOG);
  pinMode(SW, INPUT_PULLUP); 
  
}

void loop() {
  //Read Data
  xPosition = analogRead(VRx);
  yPosition = analogRead(VRy);
  SW_state = digitalRead(SW);
  mapX = map(xPosition, 0, 4095, -32767, 32767); // 修正 复位的值
  
  mapY = map(yPosition, 0, 4095, -32767, 32767);

  //Print Data in Serial Monitor - Find it at Tools > Serial Monitor
//  Serial.println(xPosition);
  Serial.print("X: ");
  Serial.print(mapX);
  Serial.print(" | Y: ");
  Serial.print(mapY);
  Serial.print(" | Button: ");
  Serial.println(SW_state);

  delay(100);
}
